package homework;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.function.Consumer;

/**
 * @classname:daemo05
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/17 20:45
 * @version: 0.1
 * @since:1.8
 */
public class demo05 {
    public static void main(String[] args) {
//        addstudent();
        showstudent();
    }

    private static void showstudent() {

        try (
                ObjectInput input = new ObjectInputStream(new FileInputStream("day16/src/dis/student.dat"))
        ) {
            ArrayList stuList = (ArrayList)input.readObject();
            stuList.forEach(new Consumer() {
                @Override
                public void accept(Object o) {
                    Students students = (Students)o;
                    students.show();
                }
            });


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void addstudent() {
        ArrayList<Students> stuList = new ArrayList<>();


        try (
                ObjectOutput output = new ObjectOutputStream(new FileOutputStream("day16/src/dis/student.dat"))
        ) {
            Scanner input = new Scanner(System.in);
            String answer;
            do {
                System.out.println("请输入学生信息");
                int id = input.nextInt();
                System.out.println("请输入学生姓名");
                String name = input.next();
                System.out.println("请输入学生性别");
                String gender = input.next();
                Students s = new Students(id, name, gender);
                stuList.add(s);
                System.out.println("是否要继续添加y/n");
                answer = input.next();
            } while ("y".equals(answer));
            output.writeObject(stuList);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
